<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="css/mui.min.css">
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a href="function.html" class=" mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">便签</h1>
			<a href="createnote.html" id="menu" class="mui-icon mui-icon-plusempty mui-pull-right" href="#topPopover"></a>
		</header>
		<div id="cdiv" class="mui-content">

		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/public.js"></script>
		<script type="text/javascript" charset="utf-8">
			var createBy = localStorage.getItem("employeeid");
			//预加载

			mui.plusReady(function() {
				mload();
			});

			//交互:数据展示
			function mload() {
				var network = true;
				if(mui.os.plus) {
					mui.plusReady(function() {
						if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
							network = false;
						}
					});
				}
				if(network) {
					console.log('网络正常,正在发送请求...');
					mui.ajax({
						url: 'http://113.71.26.27:8080/tpl/function/findnote',
						async: true,
						type: 'post',
						timeout: 2000,
						data: {
							createBy: createBy
						},
						success: function(data) {
							var tt = '';
							for(var i = 0; i < data.length; i++) {
								tt += "<div class='mui-card'><div class='mui-card-content'><div class='mui-card-footer'>" +
									"<p1 contenteditable='true' onblur=madd(" + i + "," + data[i].id + ")>" + data[i].content + "</p1>" +
									"</div></div><div class='mui-card-footer'>" +
									"<p1 id='time'>" + mmtime(data[i].createTime) + "</p1>" +
									"<button type='button' class='mui-icon mui-icon-trash mui-btn-outlined' onclick=mevent(" + data[i].id + ")></button></div></div>";
							}
							document.getElementById('cdiv').innerHTML = tt;
						}

					});
				} else {
					mui.toast("当前网络不给力，请稍后再试...");
				}

			}
			//交互:数据更新
			function madd(i, id) {
				var content = document.getElementsByTagName("p")[i].innerText;
				var network = true;
				if(mui.os.plus) {
					mui.plusReady(function() {
						if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
							network = false;
						}
					});
				}
				if(network) {
					console.log('网络正常,正在发送请求...');
					mui.ajax({
						url: 'http://113.71.26.27:8080/tpl/function/insertnote',
						async: true,
						type: 'post',
						timeout: 2000,
						data: {
							content: content,
							id: id
						},
						dataType: "text",
						success: function(data) {
							if(data == "1") {
								//列表页面
								mload();
							}
						}
						
					});
				} else {
					mui.toast("当前网络不给力，请稍后再试...");
				}
			}
			//交互:数据删除
			function mevent(id) {
				var btnArray = ['否', '是'];
				mui.confirm('是否删除所选便签？', '', btnArray, function(e) {
					if(e.index == 1) {
						mdelete(id);
					} else {

					}
				});
			}
			//ajax删除
			function mdelete(id) {
				var network = true;
				if(mui.os.plus) {
					mui.plusReady(function() {
						if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
							network = false;
						}
					});
				}
				if(network) {
					console.log('网络正常,正在发送请求...');
					mui.ajax({
						url: 'http://113.71.26.27:8080/tpl/function/deletenote',
						async: true,
						type: 'post',
						timeout: 2000,
						data: {
							id: id
						},
						success: function() {
							mload();
						}
					});
				} else {
					mui.toast("当前网络不给力，请稍后再试...");
				}
			}

			/**
			 * 重写返回事件
			 */
			mui.back = function() {
				window.location.href = "function.html";
			}
		</script>
	</body>

</html>